package com.freecloud.whispered.oss.controller;

import com.alibaba.fastjson.JSON;
import com.freecloud.framework.annotation.CORS;
import com.freecloud.framework.constant.ConfigConstant;
import com.freecloud.framework.constant.Constant;
import com.freecloud.framework.exceptions.RRException;
import com.freecloud.framework.request.Query;
import com.freecloud.framework.request.R;
import com.freecloud.framework.utils.PageUtils;
import com.freecloud.framework.validator.ValidatorUtils;
import com.freecloud.whispered.oss.config.CloudStorageConfig;
import com.freecloud.whispered.oss.entity.SysOssEntity;
import com.freecloud.whispered.oss.service.SysOssService;
import com.freecloud.whispered.oss.validator.group.AliyunGroup;
import com.freecloud.whispered.oss.validator.group.QcloudGroup;
import com.freecloud.whispered.oss.validator.group.QiniuGroup;
import com.freecloud.whispered.system.entity.SysConfigEntity;
import com.freecloud.whispered.system.service.SysConfigCallFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: maomao
 * @Date: 2019-07-31 09:37
 */
@Api
@RestController
@RequestMapping("sys/oss")
public class SysOssController {

    private final static String KEY = ConfigConstant.CLOUD_STORAGE_CONFIG_KEY;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    HttpServletRequest request;

    @Autowired
    private SysOssService sysOssService;

    @Autowired
    private SysConfigCallFactory sysConfigCallFactory;

//    @Autowired
//    private UserOssService userOssService;




    /**
     * 列表
     */
    @ApiOperation(value = "OSS列表")
    @GetMapping("/list")
    @RequiresPermissions("sys:oss:all")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Example<SysOssEntity> example = Query.getExample(params,SysOssEntity.class);
        Page<SysOssEntity> page = sysOssService.findAll(example,Query.getPageable(params,"id"));

        PageUtils pageUtil = new PageUtils(page.getContent(), Integer.parseInt(page.getTotalElements() + ""), page.getPageable().getPageSize(), page.getPageable().getPageNumber() + 1);

        return R.ok().put("page", pageUtil);
    }


    /**
     * 云存储配置信息
     */
    @ApiOperation("获取OSS配置")
    @GetMapping("/config")
    @RequiresPermissions("sys:oss:all")
    public R config(){
        SysConfigEntity config = sysConfigCallFactory.call().queryObjectByKey(KEY);
        return R.ok().put("config", config);
    }


    /**
     * 保存云存储配置信息
     */
    @ApiOperation("保存OSS云配置信息")
    @PostMapping("/saveConfig")
    @RequiresPermissions("sys:oss:all")
    public R saveConfig(@RequestBody CloudStorageConfig config){
        //校验类型
        ValidatorUtils.validateEntity(config);

        if(config.getType() == Constant.CloudService.QINIU.getValue()){
            //校验七牛数据
            ValidatorUtils.validateEntity(config, QiniuGroup.class);
        }else if(config.getType() == Constant.CloudService.ALIYUN.getValue()){
            //校验阿里云数据
            ValidatorUtils.validateEntity(config, AliyunGroup.class);
        }else if(config.getType() == Constant.CloudService.QCLOUD.getValue()){
            //校验腾讯云数据
            ValidatorUtils.validateEntity(config, QcloudGroup.class);
        }
        sysConfigCallFactory.call().updateValueByKey(KEY, JSON.toJSONString(config));
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("sys:oss:all")
    public R delete(@RequestBody Long[] ids){
        sysOssService.deleteBatch(ids);
        return R.ok();
    }

    /**
     * 用户附件
     */
    @PostMapping("/userFile")
    @RequiresPermissions("sys:oss:all")
    public R userFile(@RequestParam Map<String, Object> params){

        List<SysOssEntity> sysOssList = null;

        if(params != null && !params.isEmpty ())
        {
            sysOssList = sysOssService.queryUserFileList (params);
        }
        return R.ok().put("data", sysOssList);
    }
}

